/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.BankVirtualTrainingQue;
import com.shanhesoft.java.modules.exam.domain.dto.BankVirtualTrainingQueDto;
import com.shanhesoft.java.modules.exam.domain.dto.BankVirtualTrainingQueQueryCriteria;
import com.shanhesoft.java.modules.exam.service.BankVirtualTrainingQueService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.Set;

/**
 * 实训题目管理Controller 实训题目管理
 * @author admin
 * @version 2020-11-17 01:00:59
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/bankVirtualTrainingQue")
@Slf4j
@AllArgsConstructor
public class BankVirtualTrainingQueResource extends BaseResource {

	private final BankVirtualTrainingQueService service;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.findVoById(id));
	}
	/**
	 * GET / : get all bankVirtualTrainingQue.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all bankVirtualTrainingQue
	 */

	@PreAuthorize("@pms.hasPermission('exam_bankTrainingQue_view')")
	@GetMapping
	@ApiOperation(value = "实训题目管理查看")
	public Result getPage(PageModel pm, BankVirtualTrainingQueQueryCriteria bankVirtualTrainingQueQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, bankVirtualTrainingQueQueryCriteria);
		wrapper.eq("a.del_flag", BankVirtualTrainingQue.FLAG_NORMAL);
		wrapper.orderByDesc("a.created_date");
		return Result.buildOkData(service.findPage(pm, wrapper));
	}

	@GetMapping("/getAll")
	@ApiOperation(value = "课程实训题目查询全部")
	public Result getAllNoPage(BankVirtualTrainingQueQueryCriteria bankVirtualTrainingQueQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(bankVirtualTrainingQueQueryCriteria);
		wrapper.orderByDesc("created_date");
		return Result.buildOkData(service.list(wrapper));
	}

	@GetMapping("/getListPage")
	@ApiOperation(value = "实训题按条件查询")
	public Result getListPage(PageModel pm, @RequestParam("title") String title, @RequestParam("questionBankId") String questionBankId){

		BankVirtualTrainingQueQueryCriteria bankVirtualTrainingQueQueryCriteria = new BankVirtualTrainingQueQueryCriteria();
		if(StringUtils.isNotEmpty(title)) {
			bankVirtualTrainingQueQueryCriteria.setTitle(title);
		}
		if(StringUtils.isNotEmpty(questionBankId)) {
			bankVirtualTrainingQueQueryCriteria.setQuestionBankId(questionBankId);
		}

		bankVirtualTrainingQueQueryCriteria.setPublishFlag(BankVirtualTrainingQue.FLAG_NORMAL);

		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, bankVirtualTrainingQueQueryCriteria);
		return Result.buildOkData(service.getListPage(pm, wrapper));
	}

	/**
	 * POST / : Save a bankVirtualTrainingQueDto.
	 *
	 * @param bankVirtualTrainingQueDto the HTTP bankVirtualTrainingQue
	 */
	@PreAuthorize("@pms.hasPermission('exam_bankTrainingQue_edit')")
	@ApiOperation(value = "实训题目管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody BankVirtualTrainingQueDto bankVirtualTrainingQueDto) {
		log.debug("REST request to save BankVirtualTrainingQueDto : {}", bankVirtualTrainingQueDto);
		service.saveOrUpdate(bankVirtualTrainingQueDto);
		return Result.buildOk("保存实训题目管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" BankVirtualTrainingQue.
	 *
	 * @param ids the id of the bankVirtualTrainingQue to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_bankTrainingQue_del')")
	@ApiOperation(value = "实训题目管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete BankVirtualTrainingQue: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除实训题目管理成功");
	}

	@GetMapping("/getListPageByUserId")
	@ApiOperation(value = "编程题按条件查询")
	public Result getListPageByUserId(PageModel pm, @RequestParam("userId") String uid ){

		BankVirtualTrainingQueQueryCriteria bankVirtualTrainingQueQueryCriteria = new BankVirtualTrainingQueQueryCriteria();
		if(StringUtils.isNotEmpty(uid)) {
			bankVirtualTrainingQueQueryCriteria.setUserId(uid);
		}

		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, bankVirtualTrainingQueQueryCriteria);
		return Result.buildOkData(service.getListPageByUserId(pm, wrapper));
	}
}
